package com.hex.liyue.launchdatemodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.utils.L;

import java.util.List;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterLocation extends RecyclerView.Adapter<AdapterLocation.MyHolder> {
    private OnItemClickListener onItemClickListener;
    private List<PoiItem> list;

    public void setList(List<PoiItem> list) {
        this.list = list;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int id,String latitude,String longitude);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_location, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        PoiItem poiItem = list.get(position);
        LatLonPoint latLonPoint=poiItem.getLatLonPoint();
        final String latitude=String.valueOf(latLonPoint.getLatitude());
        final String longitude=String.valueOf(latLonPoint.getLongitude());
        L.e("位置的经纬度La",String.valueOf(latLonPoint.getLatitude()));
        L.e("位置的经纬度LOn",String.valueOf(latLonPoint.getLongitude()));

        holder.rl_item_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClickListener(position,latitude,longitude);
            }
        });
        holder.tv_name.setText(poiItem.getTitle());
        holder.tv_address.setText(poiItem.getAdName() + poiItem.getSnippet());
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_item_box;
        TextView tv_name, tv_address;

        public MyHolder(View itemView) {
            super(itemView);
            rl_item_box = (RelativeLayout) itemView.findViewById(R.id.rl_item_box);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_address = (TextView) itemView.findViewById(R.id.tv_address);
        }

    }
}
